package com.jy.wms.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jy.wms.common.base.BaseService;
import com.jy.wms.common.base.baseMenu;
import com.jy.wms.dao.LoginMapper;
import com.jy.wms.pojo.SysGys;
import com.jy.wms.pojo.SysHz;
import com.jy.wms.pojo.SysUser;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @ClassName: LoginService
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author zzp
 * @Date 2017/8/14 10:06
 */
@Service("loginService")
public class LoginService extends BaseService<LoginMapper,SysUser> {
    private List<baseMenu> list;
    public Page<SysUser> baseList(){
        EntityWrapper ew=new EntityWrapper();
        PageHelper.startPage(0,1);
        return (Page<SysUser>) dao.selectList(ew.orderBy("id"));
    }
    public JSONArray selectMenu(List<String> auth){
        return getMenu(auth);
    }

    //获取供应商
    public List<SysGys> getGys(String gysid){
        return dao.getGys(gysid);
    }
    //获取货主
    public List<SysHz> getHz(List<String> hzid, String id){
        return dao.getHz(hzid,id);
    }
    //获取所有货主2
    public List<SysHz> getHzid(List<String> hzid){
        return dao.getHz2(hzid);
    }

    //获取菜单
    public JSONArray getMenu(List<String> auth) {
        list = dao.selectMenu(auth);
        JSONArray array = new JSONArray();
        for(baseMenu menu : list){
            if("0".equals(menu.getFid())){
                JSONObject jsonObject = (JSONObject) JSONObject.toJSON(menu);
                jsonObject.put("children",build(menu.getId()));
                array.add(jsonObject);
            }
        }
        return array;
    }
    private JSONArray build(String id){
        //leave+=1;
        JSONArray buildarray = new JSONArray();
        for (baseMenu menu2 : list) {
            if(menu2.getFid().equals(id)){
                /*menu2.setLeava(leave);*/
                JSONObject jsonObject = (JSONObject) JSONObject.toJSON(menu2);
                jsonObject.put("children",build(menu2.getId()));
                buildarray.add(jsonObject);
                //lists.add(menu2);
            }
        }
        return buildarray;

    }
}
